package com.wrs.netty.protobufexample2;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int randomInt = new Random().nextInt(3);    //0~2
        MyDataInfo.MyMessage myMessage = null;
        if(0 == randomInt){
             myMessage = MyDataInfo.MyMessage.newBuilder()
                    .setDataType(MyDataInfo.MyMessage.DataType.PersonType).setPerson(
                            MyDataInfo.Person.newBuilder()
                                    .setName("张三")
                                    .setAge(20)
                                    .setAddress("北京")
                                    .build())
                    .build();
        }else if(1 == randomInt){
            myMessage = MyDataInfo.MyMessage.newBuilder()
                    .setDataType(MyDataInfo.MyMessage.DataType.DogType).setDog(
                            MyDataInfo.Dog.newBuilder()
                                    .setName("二哈")
                                    .setAge(2)
                                    .build())
                    .build();
        }else{
            myMessage = MyDataInfo.MyMessage.newBuilder()
                    .setDataType(MyDataInfo.MyMessage.DataType.CatType).setCat(
                            MyDataInfo.Cat.newBuilder()
                                    .setName("英短")
                                    .setCity("上海")
                                    .build())
                    .build();
        }

        ctx.writeAndFlush(myMessage);
    }
}
